home *** CD-ROM | disk | FTP | other *** search
- Path: informix.com!news
- From: sankar <sankar@informix.com>
- Newsgroups: comp.lang.c++
- Subject: Re: How to copy an object?
- Date: Thu, 29 Feb 1996 11:12:55 -0800
- Organization: Informix.com
- Message-ID: <3135FAB7.3D05@informix.com>
- References: <4h05rb$su6@atlas.tncnet.com>
- NNTP-Posting-Host: sankar_pc.na.informix.com
- Mime-Version: 1.0
- Content-Type: text/plain; charset=us-ascii
- Content-Transfer-Encoding: 7bit
- X-Mailer: Mozilla 2.0GoldB1 (WinNT; I)
- CC: simonl@dataworks.com
-
- Simon Lee wrote:
- >
- > Hello,
- >
- > I'm trying to have a member function return a duplicate copy of a
- > member data object. How would I go about this?
- >
- > Ie.
- >
- > class x {
- > private:
- > Y *object_of_class_y;
- >
- > public:
- > Y GetY();
- > };
- >
- > I would like GetY() to return a copy of object_of_class_y, not a pointer
- > to it.
- >
- > Any email responses appreciated.
- >
- > -Simon
-
- Hi,
- The simple solution is, return the object by value.
- Depending on the members of Y, you can decide whether you need a copy contructor in Y
- or not.
-
- Y x::GetY()
- {
- return ( *object_of_class_y );
- }
-
- This code returns a temporary copy of the object.
-
- main()
- {
- x Object1_of_x;
-
- // ........ Some code segemnt
-
- Y Object1_of_y = Object1_of_x.GetY();
-
- }
-
- The generated temporary copy will be assigned to "Object1_of_y".
-
- Thanks
- Dayashankar
-